/*
 * 
 * 
 * 
 */
package com.hboxs.asl.entity;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import com.hboxs.asl.util.DateUtils;
import com.hboxs.asl.util.HtmlUtil;

import javax.persistence.*;
import java.math.BigDecimal;
import java.util.*;

/**
 * Entity - 易问易答 - 问题
 */
@Entity
@Table(name = "xx_question")
@SequenceGenerator(name = "sequenceGenerator", sequenceName = "xx_question_sequence")
public class Question extends BaseEntity {

    private static final long serialVersionUID = 1125773224701585482L;

    /**
     * 订单编号
     */
    private String sn;

    /**
     * 问题标签
     */
    private String payType;

    /**
     * 价格
     */
    private BigDecimal payPrice;

    /**
     * 是否分享
     */
    private Boolean isShare;

    /**
     * 姓名
     */
    private String name;

    /**
     * 性别
     */
    public enum Gender {

        /**
         * 男
         */
        male,

        /**
         * 女
         */
        female;

        private static Map<String, Question.Gender> map = new HashMap<>();

        static {
            map.put("男", male);
            map.put("女", female);
        }

        @JsonCreator
        public static Question.Gender forValue(String value) {
            return map.get(value);
        }

        @JsonValue
        public String toValue() {
            for (Map.Entry<String, Question.Gender> entry : map.entrySet()) {
                if (entry.getValue() == this) {
                    return entry.getKey();
                }
            }
            return "";
        }
    }

    /**
     * 支付状态
     */
    public enum PaymentStatus {

        /**
         * 未支付
         */
        unpaid,

        /**
         * 已支付
         */
        paid,

        /**
         * 申请退款
         */
        refund,

        /**
         * 退款成功
         */
        refundSuccess;

        private static Map<String, Question.PaymentStatus> map = new HashMap<>();

        static {
            map.put("未支付", unpaid);
            map.put("已支付", paid);
            map.put("申请退款", refund);
            map.put("退款成功", refundSuccess);
        }

        @JsonCreator
        public static Question.PaymentStatus forValue(String value) {
            return map.get(value);
        }

        @JsonValue
        public String toValue() {
            for (Map.Entry<String, Question.PaymentStatus> entry : map.entrySet()) {
                if (entry.getValue() == this) {
                    return entry.getKey();
                }
            }
            return "";
        }

    }

    /**
     * 回答状态
     */
    public enum AnswerStatus {

        /**
         * 未回答
         */
        no,

        /**
         * 已回答
         */
        yes;

        private static Map<String, Question.AnswerStatus> map = new HashMap<>();

        static {
            map.put("未回答", no);
            map.put("已回答", yes);
        }

        @JsonCreator
        public static Question.AnswerStatus forValue(String value) {
            return map.get(value);
        }

        @JsonValue
        public String toValue() {
            for (Map.Entry<String, Question.AnswerStatus> entry : map.entrySet()) {
                if (entry.getValue() == this) {
                    return entry.getKey();
                }
            }
            return "";
        }

    }

    /**
     * 支付方式名称
     */
    private String paymentMethodName;

    /**
     * 支付方式
     */
    private com.hboxs.asl.entity.PaymentMethod paymentMethod;

    /**
     * 性别
     */
    private Question.Gender gender;

    /**
     * 支付状态
     */
    private Question.PaymentStatus paymentStatus;

    /**
     * 回答状态
     */
    private Question.AnswerStatus answerStatus;

    /**
     * 提问者
     */
    private Member member;

    /**
     * 被提问老师
     */
    private Member teacher;

    /**
     * 出生日期
     */
    private Date birth;

    /**
     * 出生时辰
     */
    private String birthHour;

    /**
     * 标题
     */
    private String title;

    /**
     * 问题内容
     */
    private String content;

    /**
     * 邮箱
     */
    private String email;

    /**
     * 问题图片
     */
    private List<String> images = new ArrayList<>();

    /**
     * 回答内容
     */
    private String answerContent;

    /**
     * 语音url
     */
    private String mediaURL;

    /**
     * 时长
     */
    private String timeLength;

    public String getPayType() {
        return payType;
    }

    public void setPayType(String payType) {
        this.payType = payType;
    }

    public BigDecimal getPayPrice() {
        return payPrice;
    }

    public void setPayPrice(BigDecimal payPrice) {
        this.payPrice = payPrice;
    }

    public Boolean getIsShare() {
        return isShare;
    }

    public void setIsShare(Boolean isShare) {
        this.isShare = isShare;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Question.Gender getGender() {
        return gender;
    }

    public void setGender(Question.Gender gender) {
        this.gender = gender;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    public Member getMember() {
        return member;
    }

    public void setMember(Member member) {
        this.member = member;
    }

    @JoinColumn(name = "teacher",unique = true)
    @ManyToOne(fetch = FetchType.LAZY)
    public Member getTeacher() {
        return teacher;
    }

    public void setTeacher(Member teacher) {
        this.teacher = teacher;
    }

    public Date getBirth() {
        return birth;
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }

    public String getBirthHour() {
        return birthHour;
    }

    public void setBirthHour(String birthHour) {
        this.birthHour = birthHour;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @ElementCollection
    @CollectionTable(name = "xx_question_image")
    public List<String> getImages() {
        return images;
    }

    public void setImages(List<String> images) {
        this.images = images;
    }

    public PaymentStatus getPaymentStatus() {
        return paymentStatus;
    }

    public void setPaymentStatus(PaymentStatus paymentStatus) {
        this.paymentStatus = paymentStatus;
    }

    public AnswerStatus getAnswerStatus() {
        return answerStatus;
    }

    public void setAnswerStatus(AnswerStatus answerStatus) {
        this.answerStatus = answerStatus;
    }

    public String getMediaURL() {
        return mediaURL;
    }

    public void setMediaURL(String mediaURL) {
        this.mediaURL = mediaURL;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAnswerContent() {
        return answerContent;
    }

    public void setAnswerContent(String answerContent) {
        this.answerContent = answerContent;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getTimeLength() {
        return timeLength;
    }

    public void setTimeLength(String timeLength) {
        this.timeLength = timeLength;
    }

    public String getSn() {
        return sn;
    }

    public void setSn(String sn) {
        this.sn = sn;
    }

    public String getPaymentMethodName() {
        return paymentMethodName;
    }

    public void setPaymentMethodName(String paymentMethodName) {
        this.paymentMethodName = paymentMethodName;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    public PaymentMethod getPaymentMethod() {
        return paymentMethod;
    }

    public void setPaymentMethod(PaymentMethod paymentMethod) {
        this.paymentMethod = paymentMethod;
    }

    @Transient
    public String getCreateDateF(){
        return DateUtils.date2Str(super.getCreateDate());
    }

    @Transient
    public String getAnswerContentWithoutHtml(){
        if (getAnswerContent() != null) {
            return HtmlUtil.getTextFromHtml(getAnswerContent());
        }
        return "";
    }

    @Transient
    public String getBirthDay() {
        return DateUtils.date2Str(this.getBirth());
    }

}